import Vue from 'vue'
import Vuex from 'vuex'

import {createPersistedState, createSharedMutations} from 'vuex-electron'

// const uuid = require('uuid/v4')

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentIp: '', // 当前ip
    userList: [], // 用户列表
    messageList: [], // 消息列表
    sessionList: [], // 会话
    setWindowShow: false,
    unreadCount: {} // 未读
  },
  mutations: {
    initProgram (state) {
      state.setWindowShow = false
      state.sessionList = []
      state.messageList = []
      state.userList = []
      state.currentIp = ''
      // state.unreadCount = {}
    },
    addUser (state, arg = {}) {
      let index = state.userList.findIndex(item => item.userip === arg.userip)
      if (index > -1) state.userList.splice(index, 1)
      state.userList.push(arg)
    },
    setUserList (state, arg = []) {
      state.userList = arg
    },
    setMessageList (state, arg = []) {
      state.messageList = arg
    },
    addSessionList (state, arg = '') {
      let index = state.sessionList.indexOf(arg)
      if (index < 0) state.sessionList.push(arg)
      state.currentIp = arg
    },
    removeSessionList (state, arg = '') {
      let index = state.sessionList.indexOf(arg)
      if (index < 0) return ''
      let isCurr = state.currentIp === state.sessionList[index]
      state.sessionList.splice(index, 1)
      if (isCurr) {
        if (state.sessionList[index]) {
          state.currentIp = state.sessionList[index]
        } else if (index > 0) {
          state.currentIp = state.sessionList[index - 1]
        } else {
          state.currentIp = ''
        }
      }
    },
    clearSessionList (state, arg = '') {
      state.sessionList.splice(0, state.sessionList.length)
    },
    setCurrentIp (state, arg = '') {
      state.currentIp = arg
    },
    setSetWindowShow (state, arg = false) {
      state.setWindowShow = arg
    },
    addMessageList (state, arg = false) {
      state.messageList.push(arg)
    },
    setUnreadCount (state, arg = {}) {
      if (arg.action === 'add') {
        if (state.unreadCount[arg.ip] === undefined) {
          Vue.prototype.$set(state.unreadCount, arg.ip, 0)
        }
        state.unreadCount[arg.ip]++
      } else if (arg.action === 'clear') {
        state.unreadCount[arg.ip] = 0
      }
    }
  },
  actions: {
    initProgram ({commit}) {
      commit('initProgram')
    },
    // 添加用户消息
    addUser ({commit}, arg = {}) {
      commit('addUser', arg)
    },
    // 更新用户消息
    updateUser ({commit}, arg = {}) {
      commit('addUser', arg)
    },
    setUserList ({commit}, arg = []) {
      commit('setUserList', arg)
    },
    setMessageList ({commit}, arg = []) {
      commit('setMessageList', arg)
    },
    addSessionList ({commit}, arg = '') {
      commit('addSessionList', arg)
    },
    removeSessionList ({commit}, arg = '') {
      commit('removeSessionList', arg)
    },
    clearSessionList ({commit}, arg) {
      commit('clearSessionList')
    },
    setCurrentIp ({commit}, arg = '') {
      commit('setCurrentIp', arg)
    },
    setSetWindowShow ({commit}, arg = false) {
      commit('setSetWindowShow', arg)
    },
    addMessageList ({commit}, arg = {}) {
      commit('addMessageList', arg)
    },
    setUnreadCount ({commit}, arg = {}) {
      commit('setUnreadCount', arg)
    }
  },
  getters: {
    getUserInfoByUserip (state) {
      return userip => {
        return state.userList.find(item => item.userip === userip) || {}
      }
    },
    historySessionList (state, getter) {
      let historySessionList = []
      state.messageList.map(item => {
        let user = getter.getUserInfoByUserip(item.relationuserip)
        let $item = {
          ...item,
          xm: user.xm,
          zt: user.zt,
          dnm: user.dnm,
          tx: user.tx
        }
        let index = historySessionList.findIndex(historySession => historySession.relationuserip === $item.relationuserip)
        if (index > -1) {
          if (historySessionList[index].createtime <= $item.createtime) {
            historySessionList[index] = $item
          }
        } else {
          historySessionList.push($item)
        }
      })
      return historySessionList
    },
    sessionList (state, getter) {
      return state.sessionList.map(ip => {
        let messageList = getter.getMessageListByUserip(ip)
        if (messageList.length) {
          return messageList.reduce((res, item) => {
            if (res.createtime > item.createtime) return res
            return item
          })
        }
        let user = getter.getUserInfoByUserip(ip)
        return {
          xm: user.xm,
          tx: user.tx,
          dnm: user.dnm,
          zt: user.zt,
          msgid: '',
          msg: '',
          msgtype: '',
          createtime: '',
          relationuserip: ip,
          type: ''
        }
      })
    },
    getMessageListByUserip (state, getter) {
      return userip => state.messageList.filter(item => item.relationuserip === userip).map(item => {
        let user = getter.getUserInfoByUserip(userip)
        return {
          xm: user.xm,
          tx: user.tx,
          dnm: user.dnm,
          zt: user.zt,
          ...item
        }
      })
    }
    // mineInfo (state) {
    //   // return state.userList.find(item => item.userip === commonUtil.getNetworkInfo().address)
    // }
  },
  plugins: [createPersistedState(), createSharedMutations()],
  strict: process.env.NODE_ENV !== 'production'
})
